package com.cookor.poi.modules.phy.schedule;

import com.cookor.poi.common.utils.DateUtils;
import com.cookor.poi.modules.phy.entity.TOrder;
import com.cookor.poi.modules.phy.service.TOrderService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//@Service
//@Lazy(false)
public class OrderSchedule {
  private static final Logger logger = Logger.getLogger(OrderSchedule.class);
  
  @Autowired
  private TOrderService tOrderService;
  
  @Scheduled(cron = "0 */2 * * * ?")
  @Transactional(readOnly = false, rollbackFor = {Exception.class})
  public void checkOrderStatus() throws Exception {
    logger.info("=============达到就诊时间启动=============");
    TOrder tOrder = new TOrder();
    tOrder.setStatus(Integer.valueOf(1));
    List<TOrder> list = this.tOrderService.findList(tOrder);
    List<TOrder> updateList = new ArrayList<>();
    Date now = new Date();
    for (TOrder order : list) {
      if (DateUtils.getDistanceOfTwoDate(order.getBookDate(), now) >= 1.0D)
        updateList.add(order); 
    } 
    if (!updateList.isEmpty())
      this.tOrderService.updateStatusByOrderList(updateList, 10, null); 
    logger.info("=============达到就诊时间停止=============");
  }
}
